<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!--
     通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
     通过animated属性可以开启切换标签内容时的转场动画
     通过swipeable属性可以开启滑动切换标签页
    -->
    <van-tabs v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id"
        ><articleList :channel="item" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="showFn"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑 -->
    <!-- v-model的值是控制弹窗层的显示和隐藏的，position表示从底部还是什么方向弹出；
    style表示设置弹框的高度；closeable表示设置弹窗的关闭按钮出现， close-icon-position表示弹框关闭按钮出现的位置 -->
    <van-popup
      class="edit-channel-popup"
      v-model="Show"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <channelEdit
        :myChannels="channels"
        :active="active"
        @updateActive="onUpdateActive"
      ></channelEdit>
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
// 1. 导入 获取频道列表的方法
import { getUserChannels } from "@/api/user";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage.js";
//引入组件
import articleList from "../home/components/article-list";
import channelEdit from "./components/channel-edit.vue";
export default {
  name: "HomePage",
  components: {
    articleList,
    channelEdit,
  },
  props: {},
  data() {
    return {
      active: 0,
      // 4. 定义数据接收频道列表
      channels: [],
      Show: false,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    // 3. 调用获取频道列表
    this.loadChannels();
  },
  mounted() {},
  methods: {
    // // 2. 定义加载频道列表数据的方法
    // async loadChannels() {
    //   try {
    //     const { data } = await getUserChannels();

    //     this.channels = data.data.channels;
    //     // console.log(this.channels);
    //   } catch (err) {
    //     this.$toast("获取频道列表数据失败");
    //   }
    // },

    async loadChannels() {
      try {
        // 判断用户是否已经登录，如果登录，直接向后台发送请求，获取属于用户自己的频道列表进行渲染
        // 如果用户未登录，则从本地获取修改的频道列表直接渲染
        if (this.user) {
          // 已登录，请求获取线上的频道数据
          const { data } = await getUserChannels();
          this.channels = data.data.channels;
        } else {
          // 未登录
          const localChannels = getItem("TOUTIAO_CHANNELS");
          if (localChannels) {
            // 有本地频道数据，则使用
            this.channels = localChannels;
          } else {
            // 没有本地频道数据，则请求获取默认推荐的频道列表
            const { data } = await getUserChannels();
            this.channels = data.data.channels;
          }
        }

        // // 将数据更新到组件中
        // this.channels = channels;
      } catch (err) {
        console.log(err);
        this.$toast("数据获取失败");
      }
    },

    //利用汉堡按钮控制弹窗显示状态

    showFn() {
      this.Show = true;
    },

    //切换频道点击世事件；

    onUpdateActive(index, Show = true) {
      this.active = index;
      this.Show = Show;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  // deep表示样式穿透，专门修改组件的样式
  padding-top: 100px;
  padding-bottom: 100px;
  padding-top: 175px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .van-tabs__nav {
    padding: 0;

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }
    .van-tab--active {
      .van-tab__text {
        color: #87cefa !important;
      }
    }
    .van-tabs__line {
      background: rgba(80, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
  /deep/ .van-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
  }
  /deep/.edit-channel-popup {
    .van-icon-cross {
      color: #3296fa;
    }
  }
}
</style>